<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Api\Service;
/**
 * @todo 对内api(merchant)
 * @author PHP@妖孽 <dino_ma@163.com>
 * @since 2015-9-3
 */
class MerchantService{

    protected $error = '';
    /**
     * @todo   订单查询（商户列表）
     * @param   merchant_id     商户ID
     * @param   shop_id         店铺ID
     * @param   phone_number    手机号
     * @param   page            页数
     */
    public function getShopOrderList($map){

        $result = array('status' => 0);
        if($map['merchant_id'] == 0){
            $result['info'] = 'merchant_id参数错误';
            return $result;
        }
        
        $obj = D('Api/Order');
        $list = $obj->getShopOrderList($map);
        if($list){
            $result['status'] = 1;
            $result['info'] = $list;
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
    }
    
    /**
     * @todo   订单申请核销（商户版）
     * @author fang
     */
    public function applyAccount($order_id, $merchant_id, $number){
        
        $result = array('status' => 0);
        if(empty($order_id)){
            $result['info'] = 'order_id参数错误';
            return $result;
        }
        if($merchant_id == 0){
            $result['info'] = 'merchant_id参数错误';
            return $result;
        }
        if(empty($number)){
            $result['info'] = '识别码输入错误';
            return $result;
        }
        // 申请核销时间区间 1.27（1453824000） - 2.18（1455724800）
        if(NOW_TIME > 1453824000 && NOW_TIME < 1455724800){
            $result['info'] = '尊敬的商户朋友们，1月27日至2月17日暂停申请核销功能，祝大家新年快乐';
            return $result;
        }

        $map = array(
            'order_id' => $order_id,
            'merchant_id' => $merchant_id,
            'check_code' => $number,
        );
        $obj = D('Api/Order');
        $data = $obj->applyAccount($map);
        if($data){
            $result['status'] = 1;
            $result['info'] = '“您已申请成功” 我们会对此订单进行核实，核实属实后会在5个工作日内进行返款！';
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
    }
    /**
     * @todo    订单查询接口（个人列表）
     * @param   customer_id    用户id*
     * @param   activity_id    活动id
     * @param   order_id       订单id
     * @param   page           页数
     * @author  fang
     */
    public function getOrderList($customer_id, $activity_id, $order_id, $page){
         
        $result = array('status' => 0, 'info' => 'error');
        if($customer_id <= 0){
            $result['info'] = 'customer_id参数错误';
        }
        $obj = D('Api/Order');
        $data = $obj->getOrderList($customer_id, $activity_id, $order_id, $page);
        if($data){
            $result['status'] = 1;
            $result['info'] = '';
            $result['data'] = $data;
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
    }
    
    /**
     * @todo   获取活动列表
     */
    public function getActivityList(){
        $obj = D('Api/Order');
        $list = $obj->getActivityList();
        if($list){
            $result['status'] = 1;
            $result['info'] = $list;
        }else{
            $result['status'] = 0;
            $result['info'] = $obj->getError();
        }
        $this->ajaxReturn($result);
    
    }
    
    /**
     * @todo   获取商户下的店铺列表
     */
    public function getMerchantShopList($activity_id, $merchant_id){

        $result = array('status' => 0);
        $obj = D('Api/Order');
        $list = $obj->getMerchantShopList((int)$activity_id, (int)$merchant_id);
        if($list){
            $result['status'] = 1;
            $result['info'] = $list;
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
         
    }
    
    /**
     * @todo   商户登录接口
     * @param  login_name  登录帐号
     * @param  login_pwd   登录密码
     * @author fang
     */
    public function merchantLogin($login_name){
        
        $result = array('status' => 0);
        if(empty($login_name)){
            $result['info'] = '请输入手机号码';
            return $result;
        }

        $map = array(
            'merchant_phone' => $login_name,
        );
        $obj = D('Api/Order');
        $data = $obj->merchantLogin($map);
        if($data){
            $result['status'] = 1;
            $result['info'] = $data;
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
    }
    
    /**
     * @todo    商户登录短信
     */
    public function merchantSms($login_name){
        
        $result = array('status' => 0);
        if(empty($login_name)){
            $result['info'] = '请输入手机号码';
            return $result;
        }
        
        $obj = D('Api/Order');
        $data = $obj->merchantSms($login_name);
        if($data){
            $result['status'] = 1;
            $result['info'] = $data;
        }else{
            $result['info'] = $obj->getError();
        }
        return $result;
    }
    
    /**
     * @todo    订单查询（详细）
     * @param   phone_number    手机号
     * @param   order_id        订单ID
     * @author  fang
     */
    public function getOrderInfo(){
    
        $result = array();
        $order_id = (int)I('order_id', 0);
        //$phone_number = trim(I('phone_number'));
        /* if(empty($phone_number)){
         $result['status'] = 0;
         $result['info'] = 'phone_number参数错误';
         $this->ajaxReturn($result);
        } */
        if($order_id == 0){
            $result['status'] = 0;
            $result['info'] = 'order_id参数错误';
            $this->ajaxReturn($result);
        }
    
        $map['order_id'] = $order_id;
        $obj = D('Api/Order');
         
        $data = $obj->getOrderInfo($map);
         
        if($data){
            $result['status'] = 1;
            $result['info'] = $data;
        }else{
            $result['status'] = 0;
            $result['info'] = $obj->getError();
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo 返回最后一次错误记录
     * @return string
     */
    public function getError(){
        return $this->error;
    }
    
}